<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AdminUser;
use app\admin\validate\CheckAdminUser;
use app\common\controller\AdminBaseController;
use app\common\service\GoogleAuthenticator;
use think\Exception;

class Admin extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new AdminUser();
    }


    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function addAdmin()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $checkUser = new CheckAdminUser();
            try {
                $check = $checkUser->check($data);
                if (!$check) {
                    return parent::JsonReturn($checkUser->getError(), 0);
                }
                if (isset($data['id'])) {
                    if (!empty($data['password']) && !empty($data['repassword'])) {
                        $data['password'] = password($data['password']);
                    } else {
                        unset($data['password']);
                    }
                    unset($data['repassword']);
                   
                    $username=AdminUser::where(['id' => $data['id']])->value("username");

                    if ($username!=$data['username']){
                        //谷歌验证码
                        $google=new GoogleAuthenticator();
                        $secret=$google->createSecret();
                        $qrCodeUrl = $google->getQRCodeGoogleUrl($data['username'], $secret);
                        $data['secret']=$secret;
                        $data['google_url']=$qrCodeUrl;
                    }


                } else {
                
                    $data['password'] = password($data['password']);
                    unset($data['repassword']);

                    //谷歌验证码
                    $google=new GoogleAuthenticator();
                    $secret=$google->createSecret();
                    $qrCodeUrl = $google->getQRCodeGoogleUrl($data['username'], $secret);
                    $data['secret']=$secret;
                    $data['google_url']=$qrCodeUrl;

                }
            } catch (Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
            // dd($data);
            //验证提交的数据
            return $this->model->doAll($data);
        }

        $id = $this->request->param("id/d");//父id
        $info = AdminUser::find($id);

        $this->assign(compact("info", "id"));
        return $this->fetch();
    }


    /**
     * @return string|\think\response\Json
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:修改密码
     */
    public function changPass()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            try {
                //当前密码
                $pass=AdminUser::where("id",self::$admin_info['id'])->value("password");

                if (!password_very($data['oldpassword'], $pass)) {
                    return parent::JsonReturn("当前密码错误",0);
                }
                unset($data['oldpassword']);

                $checkUser = new CheckAdminUser();
                $check = $checkUser->scene("Pass")->check($data);
                if (!$check) {
                    return parent::JsonReturn($checkUser->getError(), 0);
                }
                $data['id']=(int)self::$admin_info['id'];

                unset($data['repassword']);
                //密码加密
                $data['password']=password($data['password']);

                return $this->model->doAll($data);

            } catch (\Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }

        return $this->fetch();
    }


    /**
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:基本资料
     */
    public function baseData()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            $checkUser = new CheckAdminUser();
            try {
                $check = $checkUser->check($data);
                if (!$check) {
                    return parent::JsonReturn($checkUser->getError(), 0);
                }
                $res = AdminUser::where(['username' => "" . $data['username'] . ""])->where("id", "<>", $data['id'])->find();
                if ($res) {
                    return parent::JsonReturn($data['username'] . "该用户名已存在", 0);
                }

                return $this->model->doAll($data);

            } catch (\Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
            // dd($data);
        }
        $this->assign("info", AdminUser::find(self::$admin_info['id'])->toArray());

        return $this->fetch();
    }


}